<?php
set_time_limit(300);
defined('IN_PHPCMS') or exit('No permission resources.');
//模型缓存路径
define('CACHE_MODEL_PATH',CACHE_PATH.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
//定义在单独操作内容的时候，同时更新相关栏目页面
define('RELATION_HTML',true);

pc_base::load_app_class('admin','admin',0);
pc_base::load_sys_class('form','',0);
pc_base::load_app_func('util');
pc_base::load_sys_class('format','',0);

class company extends admin
{
	private $db, $priv_db;
	public $siteid, $categorys;

	public function __construct()
	{
		parent::__construct();
		$this->db = pc_base::load_model('company_model');
		$this->siteid = get_siteid();

	}

	public function init ()
	{

	}

	/**
	 * 机构列表
	 */
	public function index ()
	{
		$type_new = pc_base::load_model('type_new_model');
		$where = ' status = 1 AND siteid = ' . $this->siteid . ' ';
		if ($_GET['dosubmit']) {
			if ($_GET['title']) {
				$title = $_GET['title'];
				$where .= "AND `title` LIKE '%$title%' ";
			}
			if($_GET['starttime'] && $_GET['endtime']) {
				$start = strtotime($_GET['starttime']);
				$end = strtotime($_GET['endtime']);
				if($start > $end) showmessage(L('range_not_correct'),HTTP_REFERER);
				$where .= "AND createTime >= '$start' AND  createTime <= '$end' ";				
			}
			if ($_GET['classId']) $where .= " AND classId = " . $_GET['classId'];		
		}
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$company_list = $this->db->listinfo($where, 'id DESC', $page, 15);
		$pages = $this->db->pages;
		pc_base::load_sys_class('format', '', 0);
		include $this->admin_tpl('company_list');
	}

	/**
	 * 添加机构
	 */
	public function add ()
	{
		if (isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
			$company = $this->check($_POST['company']);
			$id = $this->db->insert($company, true);
			if ($id) {
				showmessage(L('add_company_success'), '?m=company&c=company&a=index');
			} else {
				showmessage(L('add_company_error'), HTTP_REFERER);
			}	
		} else {
			//获取站点模板信息
			$type_new = pc_base::load_model('type_new_model');
			pc_base::load_app_func('global', 'admin');
			include $this->admin_tpl('company_add');
		}
	}

	/**
	 * 机构修改
	 */
	public function edit (){
		if (!isset($_GET['id']) || empty($_GET['id'])) {
			showmessage(L('illegal_action'), HTTP_REFERER);
		}
		$id = intval($_GET['id']);
		if (isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
			$company = $this->check($_POST['company'], 'edit');
			$id = $this->db->update($company, array('id'=>$id, 'siteid'=>$this->siteid));
			if ($id) {
				showmessage(L('edit_company_success'), '?m=company&c=company&a=index');
			} else {
				showmessage(L('edit_company_error'), HTTP_REFERER);
			}	
		} else {
			$info = $this->db->get_one(array('id'=>$id, 'siteid'=>$this->siteid));
			extract($info);
			$type_new = pc_base::load_model('type_new_model');
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			include $this->admin_tpl('company_edit');
		}
	}

	/**
	 * 机构删除
	 */
	public function delete ()
	{
		if((!isset($_GET['id']) || empty($_GET['id'])) && (!isset($_POST['id']) || empty($_POST['id']))) {
			showmessage(L('illegal_parameters'), HTTP_REFERER);
		} else {
			if(is_array($_POST['id'])){
				foreach($_POST['id'] as $jobid_arr) {
					$this->db->update(array('status'=>'-1'),array('id'=>$jobid_arr));
				}
				showmessage(L('operation_success'),HTTP_REFERER);
			}else{
				$id = intval($_GET['id']);
				if($id < 1) return false;
				$result = $this->db->update(array('status'=>'-1'),array('id'=>$id));
				if($result){
					showmessage(L('operation_success'),HTTP_REFERER);
				}else {
					showmessage(L("operation_failure"),HTTP_REFERER);
				}
			}
		}
	}

	/**
	 * ajax检验机构名是否存在，避免机构名重复
	 */
	public function public_check_company ()
	{
		if(!$_GET['title']) exit(0);
		if(pc_base::load_config('system', 'charset')=='gbk') {
			$_GET['title'] = safe_replace(iconv('UTF-8', 'GBK', $_GET['title']));
		}
		$title = addslashes($_GET['title']);
		if($_GET['id']) {
			$id = intval($_GET['id']);
			$r = $this->db->get_one(array('id'=>$id, 'siteid' => $this->siteid));
			if($r['title'] == $title) {
				exit('1');
			}
		}
		$r = $this->db->get_one(array('siteid' => $this->siteid, 'title' => $title), 'id');
		if($r['id']) {
			exit('0');
		} else {
			exit('1');
		}
	}

	/**
	 * 检验机构名是否存在，避免机构名重复
	 */
	public function public_check_companys ($title, $id)
	{
		if(!$title) return 0;
		if(pc_base::load_config('system', 'charset')=='gbk') {
			$title = safe_replace(iconv('UTF-8', 'GBK', $title));
		}
		$title = addslashes($title);
		if($id) {
			$r = $this->db->get_one(array('id'=>$id, 'siteid' => $this->siteid));
			if($r['title'] == $title) {
				return 1;
			}
		}
		$r = $this->db->get_one(array('siteid' => $this->siteid, 'title' => $title), 'id');
		if($r['id']) {
			return 0;
		} else {
			return 1;
		}
	}

	/**
	 * 表单验证
	 * @param array $data 表单传递的值
	 * @param string $a add/edit添加操作时，自动加上默认值
	 */
	private function check($data, $a = 'add') {
		if(!$data['title']) showmessage('机构名不能为空', HTTP_REFERER);
		if(!$this->public_check_companys($data['title'],$_GET['id'])) showmessage(L('company_exist'), HTTP_REFERER);
		$data['createTime'] = $data['createTime']?strtotime($data['createTime']):SYS_TIME;
		if($a=='add') {
			$data['siteid'] = $this->siteid;
			$data['addtime'] = SYS_TIME;
			$data['addusername'] = param::get_cookie('admin_username');
			$data['adduserid'] = $_SESSION['userid'];
		} else {
			$data['updatetime'] = SYS_TIME;
		}
		return $data;
	}
}